<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
?>
<script>
require(["jquery", "prototype"], function(jQuery){

function hideShowSubproductOptions()
{
    if ($('rule_sub_is_enable').value == 1) {
        $('rule_sub_simple_action').up('div.field').show();
        $('rule_sub_discount_amount').up('div.field').show();
        $('rule_sub_discount_amount').addClassName('required-entry validate-not-negative-number');
        changeValidationForSubDiscountPercent();
    } else {
        $('rule_sub_discount_amount').
            removeClassName('required-entry').
            removeClassName('validate-not-negative-number').
            removeClassName('validate-number-range').
            removeClassName('number-range-0.00-100.00');
        $('rule_sub_simple_action').up('div.field').hide();
        $('rule_sub_discount_amount').up('div.field').hide();
    }

    return true;
}

function changeValidationForDiscountPercent()
{
    if ($('rule_simple_action').value == 'by_percent' || $('rule_simple_action').value == 'to_percent') {
        $('rule_discount_amount').addClassName('validate-number-range number-range-0.00-100.00');
    } else {
        $('rule_discount_amount').removeClassName('validate-number-range').removeClassName('number-range-0.00-100.00');
    }

    return true;
}

function changeValidationForSubDiscountPercent()
{
    if ($('rule_sub_is_enable').value == 1) {
        if ($('rule_sub_simple_action').value == 'by_percent' || $('rule_sub_simple_action').value == 'to_percent') {
            $('rule_sub_discount_amount').addClassName('validate-number-range number-range-0.00-100.00');
        } else {
            $('rule_sub_discount_amount').
                removeClassName('validate-number-range').
                removeClassName('number-range-0.00-100.00');
        }
    }

    return true;
}

jQuery(document).ready(hideShowSubproductOptions);
jQuery(document).ready(changeValidationForDiscountPercent);
jQuery(document).on('change', '#rule_sub_is_enable', hideShowSubproductOptions);
jQuery(document).on('change', '#rule_simple_action', changeValidationForDiscountPercent);
jQuery(document).on('change', '#rule_sub_simple_action', changeValidationForSubDiscountPercent);
window.hideShowSubproductOptions = hideShowSubproductOptions;
window.changeValidationForDiscountPercent = changeValidationForDiscountPercent;
window.changeValidationForSubDiscountPercent = changeValidationForSubDiscountPercent;
});
</script>
